<template>
  <div>
    <el-form ref="formRef" :model="form" :rules="rules" label-width="100px" class="demo-form">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" placeholder="请输入聘用形式">
          <el-option v-for="item in ConstantEnum.hireType" :key="item.id" :label="item.value" :value="item.id" />
          <!-- <el-option label="区域二" value="beijing" /> -->
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门名称" prop="departmentName">
        <el-input v-model="form.departmentName" placeholder="请输入部门名称" @focus="isShowTree = true" />
        <el-tree v-if="isShowTree" :data="data" :props="defaultProps" @node-click="handleNodeClick" />
      </el-form-item>
      <el-form-item label="入职时间" required>
        <el-form-item prop="timeOfEntry">
          <el-date-picker v-model="form.timeOfEntry" type="date" placeholder="选择入职时间" format="yyyy年MM月dd日" value-format="yyyy-MM-dd" style="width: 100%;" />
        </el-form-item>
      </el-form-item>
      <el-form-item label="转正时间">
        <el-form-item prop="correctionTime">
          <el-date-picker v-model="form.correctionTime" type="date" placeholder="选择转正时间" format="yyyy年MM月dd日" value-format="yyyy-MM-dd" style="width: 100%;" />
        </el-form-item>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即创建</el-button>
        <el-button @click="cancle">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// 枚举导入
import ConstantEnum from '@/constant/employees'
import { valiadeMobile } from '@/utils/validate'
import { getDepartmentInfoRequest } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import { addUserListRequest } from '@/api/employess'

export default {
  name: 'DialogAdd',
  props: {
  },
  data() {
    // 自定义规则函数
    const validateMobile = (rule, value, callback) => {
      if (!valiadeMobile(value)) {
        callback(new Error('手机号输入错误'))
      } else {
        callback()
      }
    }
    return {
      // 导入的变量直接使用时，是没有响应式的，需要在data中定义，被VUE数据劫持之后才能有响应式的
      ConstantEnum: ConstantEnum,
      form: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      // 是否展示部门的数据
      isShowTree: false,
      // 校验规则
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 4, message: '长度在 2 到 4 个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: validateMobile, trigger: 'change' }
        ],
        formOfEmployment: [
          { required: true, message: '请输入聘用形式', trigger: 'change' }
        ],
        workNumber: [
          { required: true, message: '请输入工号', trigger: 'blur' }

        ],
        departmentName: [
          { required: true, message: '请输入部门名称', trigger: 'change' }

        ],
        timeOfEntry: [
          { required: true, message: '选择入职时间', trigger: 'change' }
        ]
      },
      // 下面3个变量是 el-tree需要的
      data: [],
      defaultProps: {
        children: 'children',
        // label: 'label'
        label: 'name' // 页面上要显示的字段名称
      }
    }
  },
  created() {
    this.getDepartmentByIdRequest()
  },
  methods: {
    // 点击确认
    onSubmit() {
      console.log(this.form)
      this.$refs.formRef.validate(value => {
        console.log(value)
        if (value) {
          // 兜底校验
          this.addUserList()
        }
      })
    },
    async getDepartmentByIdRequest() {
      try {
        const res = await getDepartmentInfoRequest()
        // 删除掉数组的第一项，因为他是公司的信息
        res.data.depts.shift()
        // 将返回的值赋值给data， 并且渲染到el-tree上面； 用了一个公共的方法： 平铺结构转树形结构
        this.data = tranListToTreeData(res.data.depts, '')
        console.log('getDepartmentInfoRequest: ', res)
      } catch (err) {
        console.log('getDepartmentInfoRequest: ', err)
      }
    },
    // 点击部门名称列表
    handleNodeClick(val) {
      console.log(val)
      // 有子部门时，点击不赋值，展开列表
      if (val.children.length > 0) return
      this.form.departmentName = val.name
      this.isShowTree = false
    },
    cancle() {
      this.$emit('close')
    },
    // 异步添加请求
    async addUserList() {
      try {
        const res = await addUserListRequest(this.form)
        console.log('addUserListRequest: ', res)
        // 新增员工成功则刷新列表，关闭弹窗
        if (res.success) {
        //   this.dialogVisible = false
          this.getDepartmentByIdRequest()
          // 成功时抛出请求
          this.$emit('updateSuccess')
        }
      } catch (err) {
        console.log('addUserListRequest: ', err)
      }
    }
  }

}
</script>

<style>

</style>
